USE [Tau]
GO

/****** Object:  StoredProcedure [dbo].[editPage]    Script Date: 06/16/2011 20:58:23 ******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE [dbo].[editPage]
@pageID int,
@pageName varchar(50),
@pageTitle varchar(50),
@pageCode varchar(8000),
@bolLeft bit,
@topNavID int
AS

DECLARE @orderID int
DECLARE @topOrderID int
SET @orderID = 0
SET @topOrderID = 0

--BEGIN Check for left nav
IF @bolLeft = 1
BEGIN
--Item is in left nav, see if request is new
DECLARE @curOrderID int
SELECT @curOrderID = orderID FROM tblPages WHERE pageID = @pageID
--if new, need next available order ID
IF @curOrderID = 0 or @curOrderID IS NULL SELECT @orderID = max(orderID) + 1 FROM tblPages
--if not new, maintain current order ID
ELSE SET @orderID = @curOrderID
END
ELSE
BEGIN
--Left nav not requested, see if current exists
DECLARE @curBolLeft int
SELECT @curBolLeft = bolLeft FROM tblPages WHERE pageID = @pageID
IF @curBolLeft = 1
BEGIN
--Move all left nav items beneath current item back one
UPDATE tblPages SET orderID = orderID - 1 WHERE orderID > (SELECT orderID FROM tblPages WHERE pageID = @pageID)
END
SET @orderID = 0
END
--END Check for left nav

--BEGIN Check for top nav
IF @topNavID > 0
BEGIN
--Item is in top nav, see if request is new
DECLARE @curTopOrderID int
SELECT @curTopOrderID = topOrderID FROM tblPages WHERE pageID = @pageID
--If new, need next available order ID
IF @curTopOrderID = 0 or @curTopOrderID IS NULL SELECT @topOrderID = max(topOrderID) + 1 FROM tblPages WHERE topNavID = @topNavID
--If not new, maintain current order ID
ELSE SET @topOrderID = @curTopOrderID
END
ELSE
BEGIN
--Top nav not requested, see if current exists
DECLARE @curTopNavID int
SELECT @curTopNavID = topNavID FROM tblPages WHERE pageID = @pageID
IF @curTopNavID > 0 AND @curTopNavID IS NOT NULL
BEGIN
--Move all left nav items beneath current item back one
UPDATE tblPages SET topOrderID = topOrderID - 1 WHERE topNavID = @curTopNavID and topOrderID > (SELECT topOrderID FROM tblPages WHERE pageID = @pageID)
END
SET @topOrderID = 0
END
--END Check for top nav

UPDATE tblPages SET pageName = @pageName, pageTitle = @pageTitle, pageCode = @pageCode,
bolLeft = @bolLeft, orderID = @orderID, topNavID = @topNavID, topOrderID = @topOrderID
WHERE pageID = @pageID

GO

